
package controller;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.fabric.Response;

import dao.LogDao;
import dao.UsersDao;
import util.MybatisSessionFactory;

@Controller
public class User {
 //private Map<String, Object> result;
@RequestMapping(value="/register.action",method=RequestMethod.POST)
 
 @ResponseBody
 public String register(
		 @RequestParam(value="account",defaultValue="")String account,
		 
		 @RequestParam(value="password",defaultValue="")String password,
		 @RequestParam(value="nickname",defaultValue="")String nickname ){
	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	LogDao    dao2=Session.getMapper(LogDao.class);
   Map<String,Object> result=dao.query(account);
   Map<String,Object> map=dao.LastId();

   if("1".equals(result.get("counter").toString())){return "ERR";}
   java.util.Date utilDate = new java.util.Date();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
   String date1=formatter.format(utilDate);
   dao.add(account,password,nickname,date1);
   java.util.Date utilDate1 = new java.util.Date();
   
	SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM-dd ");
	SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
	
	String date=formatter3.format(utilDate1);
	String time=formatter2.format(utilDate1);
	System.out.println(time); 
  dao2.add2(map.get("id").toString(), "注册", "0", date, time);
   Session.commit();
   return "OK";}		 
 
 @RequestMapping(value="/login.action",method=RequestMethod.POST)
 @ResponseBody
 public String login(
		 @RequestParam(value="account",defaultValue="")String account,
		 @RequestParam(value="password",defaultValue="")String password,
		 HttpServletResponse response){
	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 LogDao    dao2=Session.getMapper(LogDao.class);
   Map<String,Object> result=dao.queryone(account,password);
   
  if("0".equals(result.get("counter").toString())){return "ERR";}
  Cookie cooki=new Cookie("user",result.get("id").toString());
  response.addCookie(cooki);
  java.util.Date utilDate = new java.util.Date();
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
	SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
	String date=formatter.format(utilDate);
	String time=formatter2.format(utilDate);
	//System.out.println(time); 
  dao2.add2(result.get("id").toString(), "登陆", "0", date, time);
  Session.commit();
   return "OK"; }
 
 
 @RequestMapping(value="/user.action",method=RequestMethod.GET)
 @ResponseBody
 public Map<String, Object> querynickname(
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response ){
      SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 Map<String,Object> map=dao.querynickname(userId);
 return map;}
 
 @RequestMapping(value="/week.action",method=RequestMethod.GET)
 @ResponseBody
 public List<Map<String,Object>> queryweek(){
     SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
   List<Map<String, Object>> list=dao.queryweek();
 return list;}
 
 @RequestMapping(value="/weekin.action",method=RequestMethod.GET)
 @ResponseBody
 public Map<String,Object> queryweekin(
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response ){
     SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
   Map<String, Object> list=dao.queryweekin(userId);
    return list;}
 
 
 @RequestMapping(value="/weekout.action",method=RequestMethod.GET)
 @ResponseBody
 public Map<String,Object> queryweekout(@CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response){
     SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
   Map<String, Object> list=dao.queryweekout(userId);
    return list;}
 
 @RequestMapping(value="/querycountin/{a}.action",method=RequestMethod.GET)
 @ResponseBody
 public List<Map<String,Object>> querycount(
		 @PathVariable int a,
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response){
     SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
   List<Map<String, Object>> list=dao.querycountin(a,userId);
   return list;}
 
 
 @RequestMapping(value="/querycountout/{a}.action",method=RequestMethod.GET)
 @ResponseBody
 public List<Map<String,Object>> querycountout(
		 @PathVariable int a,
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response ){
     SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
   List<Map<String, Object>> list=dao.querycountout(a,userId);
  return list; }
 
@RequestMapping(value="/save.action",method=RequestMethod.POST)
@ResponseBody
 public  String in(
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response,
		 @RequestParam(value="date_time",defaultValue="")String date_time,
		 @RequestParam(value="place",defaultValue="")String place,
		 @RequestParam(value="money_in",defaultValue="")String money_in,
		 @RequestParam(value="kind",defaultValue="")String kind,
		 @RequestParam(value="remark",defaultValue="")String remark
		 ){
	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 LogDao    dao2=Session.getMapper(LogDao.class);
	 Map<String,Object> map=dao.LastId();
   dao.save(userId,date_time, place, money_in, kind,remark);
   java.util.Date utilDate = new java.util.Date();
   
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
	SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
	
	String date=formatter.format(utilDate);
	String time=formatter2.format(utilDate);
	
 dao2.add2(userId.toString(), "记账收入", map.get("id").toString(), date, time);
   Session.commit();
   return "OK";}	


@RequestMapping(value="/save2.action",method=RequestMethod.POST)
@ResponseBody
 public  String out(
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response,
		 @RequestParam(value="date_time",defaultValue="")String date_time,
		 @RequestParam(value="place",defaultValue="")String place,
		 @RequestParam(value="money_out",defaultValue="")String money_out,
		 @RequestParam(value="kind",defaultValue="")String kind,
		 @RequestParam(value="remark",defaultValue="")String remark
		 ){
	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 LogDao    dao2=Session.getMapper(LogDao.class);
	 Map<String,Object> map=dao.LastId();
   dao.save2(userId,date_time, place, money_out, kind,remark);
   java.util.Date utilDate = new java.util.Date();
   
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
	SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
	
	String date=formatter.format(utilDate);
	String time=formatter2.format(utilDate);
	
dao2.add2(userId.toString(), "记账支出", map.get("id").toString(), date, time);
   Session.commit();
   return "OK";}

@RequestMapping(value="/querymonthin.action",method=RequestMethod.GET)
@ResponseBody
public Map<String,Object> querymonthin(
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response ){
    SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
  Map<String, Object> list=dao.querymonthin(userId);
   return list;}
@RequestMapping(value="/querymonthout.action",method=RequestMethod.GET)
@ResponseBody
public Map<String,Object> querymonthout(
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response ){
    SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
  Map<String, Object> list=dao.querymonthout(userId);
   return list;}
//@RequestMapping(value="/querykindin.action",method=RequestMethod.GET)
//@ResponseBody
//public List<Map<String,Object>> querykindin(
//		 @CookieValue(value = "user",defaultValue="")String userId,
//		 HttpServletResponse response ){
//    SqlSession Session=MybatisSessionFactory.getSqlSession();
//	 UsersDao dao=Session.getMapper(UsersDao.class);
//	 List<Map<String,Object>> list=dao.querykindin(userId);
//   return list;}
//@RequestMapping(value="/querykindout.action",method=RequestMethod.GET)
//@ResponseBody
//public List<Map<String,Object>> querykindout(
//		 @CookieValue(value = "user",defaultValue="")String userId,
//		 HttpServletResponse response ){
//    SqlSession Session=MybatisSessionFactory.getSqlSession();
//	 UsersDao dao=Session.getMapper(UsersDao.class);
//	 List<Map<String,Object>> list=dao.querykindout(userId);
//   return list;}

@RequestMapping(value="/chaxun.action",method=RequestMethod.POST)
@ResponseBody
public List<Map<String, Object>> chaxun(
		 
		 @RequestParam(value="date_time",defaultValue="")String date_time,
		 @RequestParam(value="place",defaultValue="")String place,
		 @RequestParam(value="money_in",defaultValue="")String money_in,
		 @RequestParam(value="money_out",defaultValue="")String money_out,
		 @RequestParam(value="kind",defaultValue="")String kind,
		 @RequestParam(value="remark",defaultValue="")String remark,
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response){
	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	
  List<Map<String,Object>> result=dao.chaxun(date_time,place,money_in,money_out,kind,remark,userId);
  
 //if("0".equals(result.get("counter").toString())){return "ERR";}
// Cookie cooki=new Cookie("user",result.get("id").toString());
// response.addCookie(cooki);

 Session.commit();
  return result; 
  
  }

@RequestMapping(value="/zongji.action",method=RequestMethod.GET)
@ResponseBody
public List<Map<String,Object>> zongji(
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response ){
    SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 List<Map<String,Object>> list=dao.zongji(userId);
   return list;}
@RequestMapping(value="/zongji2.action",method=RequestMethod.GET)
@ResponseBody
public List<Map<String,Object>> zongji2(
		 @CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response ){
    SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 List<Map<String,Object>> list=dao.zongji2(userId);
   return list;}

@RequestMapping(value="/exit.action",method=RequestMethod.POST)
@ResponseBody
public String exit(
		@CookieValue(value = "user",defaultValue="")String userId,
		HttpServletResponse response ){
	 
	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	
	 LogDao    dao2=Session.getMapper(LogDao.class);
 
	 
	 java.util.Date utilDate = new java.util.Date();
	 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
		SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
		String date=formatter.format(utilDate);
		String time=formatter2.format(utilDate);
	 dao2.add2(userId, "退出", "0", date, time);
 Cookie cooki=new Cookie("user",null);
 response.addCookie(cooki);

 Session.commit();
  return "OK"; }

@RequestMapping(value="/querymore.action",method=RequestMethod.GET)
@ResponseBody
public List<Map<String,Object>> querymore(
		@RequestParam(value="id",defaultValue="")String id,
		@CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response){
    SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 List<Map<String,Object>> list=dao.querymore(id,userId);
   return list;}


@RequestMapping(value="/del.action",method=RequestMethod.POST)
@ResponseBody
public String del(
		@RequestParam(value="id",defaultValue="")String id,
		@CookieValue(value = "user",defaultValue="")String userId,
		 HttpServletResponse response){
	 System.out.println("aaaaaaaaaaaaaaaa");
    SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 LogDao    dao2=Session.getMapper(LogDao.class);
	 java.util.Date utilDate = new java.util.Date();
	  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
		SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
		String date=formatter.format(utilDate);
		String time=formatter2.format(utilDate);
		//System.out.println(time); 
	  dao2.add2(userId, "删除", id, date, time);
	 dao.del(id, userId);
	 Session.commit();
   return "OK";}
}
